package com.inspur.security.cbb3.kms.keymanager.secretsstore.service.impl;

import com.inspur.security.cbb3.kms.keymanager.secrets.po.SecretsBean;
import com.inspur.security.cbb3.kms.keymanager.secretsstore.po.SecretsStoreBean;
import com.inspur.security.cbb3.kms.keymanager.secretsstore.repository.SecretsStoreRepository;
import com.inspur.security.cbb3.kms.keymanager.secretsstore.service.SecretsStoreService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author: lijunchang
 * @Description: 容器密钥关联关系service实现
 * @Date: 2020/10/19
 */
@Service
@Slf4j
@Transactional(readOnly = true)
public class SecretsStoreServiceImpl implements SecretsStoreService {

    @Autowired
    private SecretsStoreRepository secretsStoreRepository;

    /**
     * 根据密钥表信息、密文信息进行存储表新增
     *
     * @param secretsBean         密钥表信息
     * @param ciphertextSecretKey 密文信息
     * @return 密钥存储表信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public SecretsStoreBean saveSecretsStoreBean(SecretsBean secretsBean, String ciphertextSecretKey) {
        SecretsStoreBean secretsStoreBean = new SecretsStoreBean();
        secretsStoreBean.setDeleted(false);
        secretsStoreBean.setCreatedTime(secretsBean.getCreatedTime());
        secretsStoreBean.setSecretId(secretsBean.getUuid());
        secretsStoreBean.setCypherText(ciphertextSecretKey);
        return secretsStoreRepository.save(secretsStoreBean);
    }
}
